local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_event_item_buyer"] or {}
end



local  npc_components_for_sale = Class(function(self, inst)
	self.inst = inst
	self.inst.npc_components_for_sale = self
end)



local function chesspiece_item_data_set(inst,target,str_table)
	local Statue_name = str_table[2]
	local Material = str_table[3] or "marble"	------ stone:石砖（黑色）   moonglass:玻璃     nil:大理石  marble？？
	-- -- 默认大理石
	print(Statue_name,Material)
	target.npc_everything_data:Set("type","chesspiece")
	target.npc_everything_data:Set("reward_name","goldnugget")
	--------------------------------------------------------------------
	--- 材料价格
	if Material == "marble" then
		target.npc_everything_data:Add("reward_num",math.random(5))
	elseif Material == "moonglass" then
		target.npc_everything_data:Add("reward_num",math.random(10))
	else
		target.npc_everything_data:Add("reward_num",math.random(3))
	end	
	--------------------------------------------------------------------

	target.npc_everything_data:Add("reward_num",math.random(5))
	target.npc_base_lib:For_Sale_Sign_Show()
	inst:Remove()
end

local function glass_from_boss_data_set(inst,target)
	target.npc_everything_data:Set("reward_name","goldnugget")
	target.npc_everything_data:Add("reward_num",math.random(15))
	target.npc_everything_data:Set("type","glass")
	target.npc_base_lib:For_Sale_Sign_Show()
	inst:Remove()	
end

local function oversized_veggie_data_set(inst,target)
	target.npc_everything_data:Set("reward_name","goldnugget")

	if target.components.perishable then
		target.npc_everything_data:Add("reward_num",math.random(12,15))
		target.components.perishable.perishtime = target.components.perishable.perishtime*10
		target.components.perishable.perishremainingtime = target.components.perishable.perishremainingtime*10
	else
		target.npc_everything_data:Add("reward_num",math.random(20,30))
	end
	target.npc_everything_data:Set("type","veggie")
	target.npc_base_lib:For_Sale_Sign_Show()
	inst:Remove()
end

local function house_data_set(inst,target)
	target.npc_everything_data:Set("type","house")

	if target.prefab == "npc_item_walrus_camp" then
		target.npc_everything_data:Set("reward_name","goldnugget")
		target.npc_everything_data:Add("reward_num",math.random(300,500))
	else
		target.npc_everything_data:Set("reward_name","goldnugget")
		target.npc_everything_data:Add("reward_num",math.random(12,15))
	end

	target.npc_base_lib:For_Sale_Sign_Show()
	target:PushEvent("tag_for_sale_task")
	inst:Remove()
end

local function sunkenchest_data_set(inst,target)
	target.npc_everything_data:Set("type","sunkenchest")
	target.npc_everything_data:Set("reward_name","goldnugget")
	target.npc_everything_data:Add("reward_num",math.random(110,200))
	target.npc_base_lib:For_Sale_Sign_Show()
	target:PushEvent("tag_for_sale_task")
	inst:Remove()
end
local function shell_cluster_data_set(inst,target)
	target.npc_everything_data:Set("type","shell_cluster")
	target.npc_everything_data:Set("reward_name","goldnugget")
	target.npc_everything_data:Add("reward_num",math.random(10,30))
	target.npc_base_lib:For_Sale_Sign_Show()
	target:PushEvent("tag_for_sale_task")
	inst:Remove()
end

local function birdcage_data_set(inst,target)
	target.npc_everything_data:Set("type","birdcage")
	target.npc_everything_data:Set("reward_name","goldnugget")
	target.npc_everything_data:Add("reward_num",math.random(7,10))
	target.npc_base_lib:For_Sale_Sign_Show()
	target:PushEvent("tag_for_sale_task")
	inst:Remove()
end

local markable = {
	["chesspiece"] = true,
	["glassspike"] = true,	---- 蚁狮
	["glassblock"] = true,	---- 蚁狮
	["sunkenchest"]	= true,	----- 沉船宝箱
	["shell_cluster"] = true, ---- 贝壳堆
	["pighouse"] = true,
	["rabbithouse"] = true,
	["npc_item_walrus_camp"] = true,
	["birdcage"] = true
}

function npc_components_for_sale:Check_Target(target)
	if target:HasTag("for_sale") then
		return false,"SELLING"
	end
	if target:HasTag("burnt") then
		return false,"NON_SALE_ITEM"
	end

	local splited_str_table = {}
	if target.components.inventoryitem and type(target.components.inventoryitem.imagename) == "string" then
		splited_str_table = self.inst.npc_base_lib:Str_Split(target.components.inventoryitem.imagename,"_")
	else
		splited_str_table = self.inst.npc_base_lib:Str_Split(target.prefab,"_")
	end
	local item_type = splited_str_table[1]
	if item_type == nil or not(markable[item_type] or markable[target.prefab] or target:HasTag("oversized_veggie") ) then		
		return false,"NON_SALE_ITEM"
	end
	print("Check_Target:",target.prefab)
	------------------------------------------------------------------------------------------
	local sign_type = self.inst.npc_everything_data:Get("type")
	------------------------------------------------------------------------------------------
	------ 雕像制品
	if item_type == "chesspiece" and sign_type == "chesspiece"  then
		chesspiece_item_data_set(self.inst,target,splited_str_table)
		return true
	end
	------------------------------------------------------------------------------------------
	--- --- 蚁狮玻璃制品
	if sign_type == "glass" and (item_type == "glassspike" or item_type == "glassblock") then
		glass_from_boss_data_set(self.inst,target)
		return true
	end
	------------------------------------------------------------------------------------------
	-- -- -- 巨大植物
	if sign_type == "veggie" and target:HasTag("oversized_veggie") then
		oversized_veggie_data_set(self.inst,target)
		return true
	end
	------------------------------------------------------------------------------------------
	-------- 猪人 兔人 自制海象房 房子
	if sign_type == "house" and target:HasTag("onbuilt_by_player") 
	and ( target.prefab == "rabbithouse" or target.prefab == "pighouse" or target.prefab == "npc_item_walrus_camp" ) then
		house_data_set(self.inst,target)
		return true
	end
	------------------------------------------------------------------------------------------
	if sign_type == "sunkenchest" and target.prefab == "sunkenchest" then
		sunkenchest_data_set(self.inst,target)
		return true
	end
	------------------------------------------------------------------------------------------
	if sign_type == "shell_cluster" and target.prefab == "shell_cluster" then
		shell_cluster_data_set(self.inst,target)
		return true
	end
	------------------------------------------------------------------------------------------
	if sign_type == "birdcage" and target.prefab == "birdcage" then
		birdcage_data_set(self.inst,target)
		return true
	end
	------------------------------------------------------------------------------------------
	-- if markable[item_type] or markable[target.prefab] then
	-- 	print("info : other type, sale makr on",target)
	-- 	return true
	-- end
	------------------------------------------------------------------------------------------
	-- print("markable[item_type] == nil")
	return false
end


function npc_components_for_sale:MessageForPlayer(doer)
	if not TheWorld.ismastersim or doer == nil or doer.npc_base_lib == nil or doer.npc_everything_data == nil then
		return
	end
	local display_falg = false
	if doer.npc_everything_data:Get("npc_components_for_sale.MessageForPlayer") ~= true then
		display_falg = true
		doer.npc_everything_data:Set("npc_components_for_sale.MessageForPlayer",true)
	end

	if math.random(100) < 20 then
		display_falg = true
	end

	if display_falg then
		doer.npc_base_lib:Wisper(GetStringsTable()["Instructions"])
	end

end

return npc_components_for_sale
